<%@page import="com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>
<%@ include file="/jsps/init.jsp"%>
<%
	int detal = 10;
	UserSongs searchUserSongs = UserSongsLocalServiceUtil.createUserSongs(0);
	
	boolean isRating = "rating".equals(orderByCol);
	boolean isHotList="hotList".equals(orderByCol);
	boolean userProfile="userProfile".equals(orderByCol);
	
	String 
	songName = ParamUtil.getString(request, "songName", StringPool.BLANK), 
	songAuthor = ParamUtil.getString(request, "songAuthor", StringPool.BLANK), 
	songSinger = ParamUtil.getString(request, "songSinger", StringPool.BLANK), 
	recordNumber = ParamUtil.getString(request, "recordNumber", StringPool.BLANK);
	Integer songCategory = ParamUtil.getInteger(request, "songCategory", 0); 
	
	Song searchSong = SongLocalServiceUtil.createSong(0);
	searchSong.setRecordNumber(-1);
	searchSong.setSongAuthor(songAuthor);
	searchSong.setSongCategory(songCategory);
	searchSong.setSongName(songName);
	searchSong.setSongSinger(songSinger);
	
	boolean isSignedIn= themeDisplay.isSignedIn();
	
	//x = RatingsEntryLocalServiceUtil.dynamicQuery().setProjection(ProjectionFactoryUtil.sum("score")).add(PropertyFactoryUtil.forName("userId").eq(10153L)).list();
	
%>

<!-- <div> -->
<%-- 	<h1 class="karaokeList"><c:out value="<%=title %>"></c:out></h1> --%>
<!-- </div> -->
<div style="padding: 10px">
	<ul class="song-list">
		<liferay-ui:search-container emptyResultsMessage="no-song-found">
			<c:if test="<%= !isRating && !isHotList && !userProfile%>">
				<liferay-ui:search-container-results>
					<%
			results = SongServiceUtil.search(searchSong, searchContainer.getStart(), searchContainer.getEnd(), orderByCol, orderByType);
			results = ListUtil.copy(results);
			total = SongServiceUtil.searchCount(searchSong);
			
			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
			%>
				</liferay-ui:search-container-results>

				<liferay-ui:search-container-row
					className="com.mto.module.karaoke.model.Song" modelVar="song"
					keyProperty="songId">
					<%
// 			Song song = SongLocalServiceUtil.getSong(userSongs.getSongId());
// 			User songUser = UserLocalServiceUtil.getUser(userSongs.getUserId());
			
			String viewUrl = renderResponse.createRenderURL().toString();
			viewUrl = HttpUtil.setParameter(viewUrl, "p_p_id", prefix);
			viewUrl = HttpUtil.setParameter(viewUrl, "_" + prefix + "_tab", "phong-thu");
			viewUrl = HttpUtil.setParameter(viewUrl, "_" + prefix + "_songId", String.valueOf(song.getSongId()));
			viewUrl = HttpUtil.setParameter(viewUrl, "_" + prefix + "_jspPage", "/jsps/phong-thu/record.jsp");
			String cssClass = "";
			if(index == 0) cssClass += " first"; 
			if(index == total - 1) cssClass += " last";
			%>
					<li class="even-row <%= cssClass%>">
						<div class="songBox">
							<span class="songBox-img"><img alt=""
								src="<%= imgPath + "icon_notnhac.gif"%>"></span> <span
								class="text"><a href="<%= viewUrl%>"><%=song.getSongName() %></a></span><br>
							<span class="text lpad-20px"><%=song.getSongSinger() %></span> <span
								class="songBox-img-2"><img alt=""
								src="<%= imgPath + "icon_loa.gif"%>"></span>
						</div>
					</li>
				</liferay-ui:search-container-row>
			</c:if>

			<c:if test="<%= isRating && !isHotList && !userProfile%>">
				<liferay-ui:search-container-results>
					<%
			long classNameId = PortalUtil.getClassNameId(UserSongs.class);
			DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(RatingsEntry.class);
			dynamicQuery.addOrder(OrderFactoryUtil.desc("score"));
			dynamicQuery.add(PropertyFactoryUtil.forName("classNameId").eq(classNameId));
			results = RatingsEntryLocalServiceUtil.dynamicQuery(dynamicQuery, searchContainer.getStart(), searchContainer.getEnd());
			total = (int)RatingsEntryLocalServiceUtil.dynamicQueryCount(dynamicQuery);
			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
			%>
				</liferay-ui:search-container-results>

				<liferay-ui:search-container-row
					className="com.liferay.portlet.ratings.model.RatingsEntry"
					modelVar="ratingsEntry" keyProperty="entryId">
					<%
			long userSongId = ratingsEntry.getClassPK();
			UserSongs userSongs = UserSongsLocalServiceUtil.getUserSongs(userSongId);
			Song song = SongLocalServiceUtil.getSong(userSongs.getSongId());
			User songUser = UserLocalServiceUtil.getUser(userSongs.getUserId()); 
			%>
					<li class="clearfix <%= index%2==0?"even-row":"odd-row"%>">
						<liferay-ui:user-display userId="<%= songUser.getUserId() %>" /> 
						<c:out value="<%=song.getSongName() %>"></c:out>
					</li>
				</liferay-ui:search-container-row>
			</c:if>

			<c:if test="<%=isHotList %>">
				<li>
					<div class="hotListBox">
<%-- 						<img alt="" src="<%= imgPath + "icon_notnhac.gif"%>">  --%>
						<span
							class="text"> <a href="">Bai thu moi</a>
						</span><br>

					</div>
				</li>
				<li>
					<div class="hotListBox">
<%-- 						<img alt="" src="<%= imgPath + "icon_notnhac.gif"%>">  --%>
						<span
							class="text"> <a href="">Karaoke moi</a>
						</span><br>

					</div>
				</li>
				<li>
					<div class="hotListBox">
<%-- 						<img alt="" src="<%= imgPath + "icon_notnhac.gif"%>">  --%>
						<span
							class="text"> <a href="">Bai thu hot</a>
						</span><br>

					</div>
				</li>
			</c:if>
		</liferay-ui:search-container>
	</ul>
	
	<c:if test="<%=!isHotList && !userProfile%>">
				<div class="link-next">
			<%
			String viewUrl = renderResponse.createRenderURL().toString();
			viewUrl = HttpUtil.setParameter(viewUrl, "p_p_id", prefix);
			viewUrl = HttpUtil.setParameter(viewUrl, "_" + prefix + "_tab", "phong-thu");	
			viewUrl = HttpUtil.setParameter(viewUrl, "_" + prefix + "_jspPage", "/jsps/phong-thu/view.jsp");
			%>
				<a href="<%= viewUrl %>"><liferay-ui:message key="xem-tiep" /></a>
			</div>
	</c:if>
	
	
	
	<c:if test="<%=userProfile && isSignedIn%>">
	<%
		User userLogin = UserLocalServiceUtil.getUser(themeDisplay.getUserId()); 
		String username= userLogin.getUuid();
	%>
		<span>Chào bạn <%=  username%></span>		
<%-- 		<liferay-ui:user-display userId="<%=username %>"></liferay-ui:user-display> --%>
	
	</c:if>
	
	<c:if test="<%=userProfile && !isSignedIn%>">
		chua login
	
	</c:if>
	
</div>